import 'package:flutter/material.dart';
import 'package:food_hub_app/apis/recipe.dart';
import 'package:food_hub_app/models/recipe.dart';
import 'package:food_hub_app/widgets/recipe/card.dart';
import 'package:tdesign_flutter/tdesign_flutter.dart';

class RecipeList extends StatefulWidget {
  const RecipeList({super.key});

  @override
  State<RecipeList> createState() => _RecipeListState();
}

class _RecipeListState extends State<RecipeList> {
  List<RecipeSummary> recipeSummaryList = [];

  @override
  void initState() {
    super.initState();
    refreshRecipeList();
  }

  Future<void> refreshRecipeList() async {
    final result = await queryRecipeApi(RecipeQuery(category: ""));

    setState(() {
      recipeSummaryList = result;
    });
  }

  @override
  Widget build(BuildContext context) {
    if (recipeSummaryList.isEmpty) {
      return const TDEmpty(type: TDEmptyType.plain, emptyText: '暂无数据');
    } else {
      return ListView.builder(
        itemCount: recipeSummaryList.length,
        itemBuilder: (context, index) {
          return RecipeCard(recipe: recipeSummaryList[index]);
        }
      );
    }
  }
}
